package com.cooperate.fly.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cooperate.fly.bo.Case;
import com.cooperate.fly.bo.CaseWithBLOBs;
import com.cooperate.fly.bo.Field;
import com.cooperate.fly.bo.TMatrixX;
import com.cooperate.fly.bo.TMatrixY;
import com.cooperate.fly.bo.TMatrixZ;
import com.cooperate.fly.bo.TMatrixZWithBLOBs;
import com.cooperate.fly.bo.User;
import com.cooperate.fly.mapper.CaseMapper;
import com.cooperate.fly.mapper.FieldMapper;
import com.cooperate.fly.mapper.TMatrixXMapper;
import com.cooperate.fly.mapper.TMatrixYMapper;
import com.cooperate.fly.mapper.TMatrixZMapper;
import com.cooperate.fly.mapper.TableMapper;
import com.cooperate.fly.service.system.Constant;
import com.cooperate.fly.web.Result;

@Controller
@RequestMapping("/table")
public class TableController extends BaseController {
	@Autowired
	TableMapper tableMapper;
	@Autowired
	CaseMapper caseMapper;
	@Autowired
	FieldMapper fieldMapper;

	@Autowired
	TMatrixXMapper tMatrixXMapper;
	@Autowired
	TMatrixYMapper tMatrixYMapper;
	@Autowired
	TMatrixZMapper tMatrixZMapper;

	@Value("#{propertiesReader['field_data_float_type']}")
	public int fieldFloatType;

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Result update(HttpSession session, HttpServletRequest request) {
		Result res = new Result();
		int caseId = Integer.parseInt(request.getParameter("caseId"));
		int fieldId = Integer.parseInt(request.getParameter("fieldId"));
		String value = request.getParameter("value");
		Case _case = caseMapper.selectByPrimaryKey(caseId);
		tableMapper.updateValue("table_" + _case.getSchemaId(), value, fieldId,
				caseId);
		// 更新case_definition修改时间及修改人
		User user = (User) session.getAttribute(Constant.USER_SESSION_KEY);
		CaseWithBLOBs caseWithBLOBs = new CaseWithBLOBs();
		caseWithBLOBs.init(_case);
		caseWithBLOBs.setReviser(user.getUserName());
		caseMapper.updateTime(caseWithBLOBs);
		res.setMessage(value + "     "
				+ fieldMapper.selectByPrimaryKey(fieldId).getRemark3());
		// System.out.println(res.getMessage());
		res.setSuccessful(true);
		return res;
	}

	@RequestMapping(value = "/updateRemark", method = RequestMethod.POST)
	@ResponseBody
	public Result updateRemark(HttpSession session, HttpServletRequest request) {
		Result res = new Result();
		int caseId = Integer.parseInt(request.getParameter("caseId"));
		int fieldId = Integer.parseInt(request.getParameter("fieldId"));
		String value = request.getParameter("remark");
		Case _case = caseMapper.selectByPrimaryKey(caseId);
		tableMapper.updateRemark("table_" + _case.getSchemaId(), value,
				fieldId, caseId);
		// 更新case_definition修改时间及修改人
		User user = (User) session.getAttribute(Constant.USER_SESSION_KEY);
		CaseWithBLOBs caseWithBLOBs = new CaseWithBLOBs();
		caseWithBLOBs.init(_case);
		caseWithBLOBs.setReviser(user.getUserName());
		caseMapper.updateTime(caseWithBLOBs);
		res.setSuccessful(true);
		return res;
	}

	@RequestMapping(value = "/getValue", method = RequestMethod.POST)
	@ResponseBody
	public Result getValue(HttpServletRequest request) {
		Result res = new Result();
		int caseId = Integer.parseInt(request.getParameter("caseId"));
		int fieldId = Integer.parseInt(request.getParameter("fieldId"));
		Case _case = caseMapper.selectByPrimaryKey(caseId);
		String value = tableMapper.selectByCaseIdAndFieldId(
				"table_" + _case.getSchemaId(), caseId, fieldId).getValue();
		res.setMessage(value);
		res.setSuccessful(true);
		return res;
	}

	@RequestMapping(value = "/getFloatUnit", method = RequestMethod.POST)
	@ResponseBody
	public Result getFloatUnit(HttpServletRequest request) {
		Result res = new Result();
		int caseId = Integer.parseInt(request.getParameter("caseId"));
		Case _case = caseMapper.selectByPrimaryKey(caseId);
		List<Field> fields = fieldMapper.selectBySchemaId(_case.getSchemaId());
		StringBuilder s = new StringBuilder();
		for (Field field : fields) {
			if (field.getType() == fieldFloatType) {
				s.append(field.getId() + "," + field.getRemark3() + ";");
			}
		}
		s.deleteCharAt(s.length() - 1);
		res.setMessage(s.toString());
		// System.out.println(res.getMessage());
		res.setSuccessful(true);
		return res;
	}

	@RequestMapping(value = "/getMatrixHead", method = RequestMethod.POST)
	@ResponseBody
	public Result getMatrixHead(HttpServletRequest request) {
		Result res = new Result();
		int fieldId = Integer.parseInt(request.getParameter("fieldId"));
		Field field = fieldMapper.selectByPrimaryKey(fieldId);
		String head = field.getRemark3();
		res.setMessage(head);
		res.setSuccessful(true);
		return res;
	}

	@RequestMapping(value = "/get3MatrixXHead", method = RequestMethod.POST)
	@ResponseBody
	public Result get3MatrixXHead(HttpServletRequest request) {
		Result res = new Result();
		int fieldId = Integer.parseInt(request.getParameter("fieldId"));
		TMatrixX tMatrixX = tMatrixXMapper.selectByCaseIdAndFieldId(null,
				fieldId);
		String head = tMatrixX.getValue();
		head = head.replaceAll(">", "＞");
		head = head.replaceAll("~", "～");
		head = head.replaceAll("<", "＜");
		String name = tMatrixX.getName();
		res.setMessage(name + "#" + head);
		res.setSuccessful(true);
		return res;
	}

	@RequestMapping(value = "/get3MatrixYHead", method = RequestMethod.POST)
	@ResponseBody
	public Result get3MatrixYHead(HttpServletRequest request) {
		Result res = new Result();
		int fieldId = Integer.parseInt(request.getParameter("fieldId"));
		TMatrixY tMatrixY = tMatrixYMapper.selectByCaseIdAndFieldId(null,
				fieldId);
		String head = tMatrixY.getValue();
		head = head.replaceAll(">", "＞");
		head = head.replaceAll("~", "～");
		head = head.replaceAll("<", "＜");
		String name = tMatrixY.getName();
		res.setMessage(name + "#" + head);
		res.setSuccessful(true);
		return res;
	}

	@RequestMapping(value = "/get3MatrixZHead", method = RequestMethod.POST)
	@ResponseBody
	public Result get3MatrixZHead(HttpServletRequest request) {
		Result res = new Result();
		int fieldId = Integer.parseInt(request.getParameter("fieldId"));
		TMatrixX tMatrixX = tMatrixXMapper.selectByCaseIdAndFieldId(null,
				fieldId);
		TMatrixY tMatrixY = tMatrixYMapper.selectByCaseIdAndFieldId(null,
				fieldId);
		String head = tMatrixX.getName() + "#" + tMatrixX.getValue() + "*"
				+ tMatrixY.getName() + "#" + tMatrixY.getValue();
		head = head.replaceAll(">", "＞");
		head = head.replaceAll("~", "～");
		head = head.replaceAll("<", "＜");
		res.setMessage(head);
		res.setSuccessful(true);
		return res;
	}

	@RequestMapping(value = "/get3MatrixXValue", method = RequestMethod.POST)
	@ResponseBody
	public Result get3MatrixXValue(HttpServletRequest request) {
		Result res = new Result();
		int caseId = Integer.parseInt(request.getParameter("caseId"));
		int fieldId = Integer.parseInt(request.getParameter("fieldId"));
		TMatrixX tMatrixX = tMatrixXMapper.selectByCaseIdAndFieldId(caseId,
				fieldId);
		String value = tMatrixX.getValue();
		res.setMessage(value);
		res.setSuccessful(true);
		return res;
	}

	@RequestMapping(value = "/get3MatrixYValue", method = RequestMethod.POST)
	@ResponseBody
	public Result get3MatrixYValue(HttpServletRequest request) {
		Result res = new Result();
		int caseId = Integer.parseInt(request.getParameter("caseId"));
		int fieldId = Integer.parseInt(request.getParameter("fieldId"));
		TMatrixY tMatrixY = tMatrixYMapper.selectByCaseIdAndFieldId(caseId,
				fieldId);
		String value = tMatrixY.getValue();
		res.setMessage(value);
		res.setSuccessful(true);
		return res;
	}

	@RequestMapping(value = "/get3MatrixZValue", method = RequestMethod.POST)
	@ResponseBody
	public Result get3MatrixZValue(HttpServletRequest request) {
		Result res = new Result();
		int caseId = Integer.parseInt(request.getParameter("caseId"));
		int fieldId = Integer.parseInt(request.getParameter("fieldId"));
		TMatrixZWithBLOBs tMatrixZ = tMatrixZMapper.selectByCaseIdAndFieldId(
				caseId, fieldId);
		String value = "";
		if (tMatrixZ != null) {
			value = tMatrixZ.getValueX();
		}
		res.setMessage(value);
		res.setSuccessful(true);
		return res;
	}

	@RequestMapping(value = "/updateMatrixX", method = RequestMethod.POST)
	@ResponseBody
	public Result updateMatrixX(HttpServletRequest request) {
		Result res = new Result();
		int caseId = Integer.parseInt(request.getParameter("caseId"));
		int fieldId = Integer.parseInt(request.getParameter("fieldId"));
		String value = request.getParameter("value");
		TMatrixX head = tMatrixXMapper.selectByCaseIdAndFieldId(null, fieldId);
		TMatrixX rec = new TMatrixX();
		rec.setCaseid(caseId);
		rec.setFid(fieldId);
		rec.setName(head.getName());
		rec.setValue(value);
		TMatrixX tMatrixX = tMatrixXMapper.selectByCaseIdAndFieldId(caseId,
				fieldId);
		if (tMatrixX == null) {
			// insert
			tMatrixXMapper.insert(rec);
		} else {
			// update
			rec.setId(tMatrixX.getId());
			tMatrixXMapper.updateByPrimaryKeyWithBLOBs(rec);
		}
		res.setSuccessful(true);
		return res;
	}

	@RequestMapping(value = "/updateMatrixY", method = RequestMethod.POST)
	@ResponseBody
	public Result updateMatrixY(HttpServletRequest request) {
		Result res = new Result();
		int caseId = Integer.parseInt(request.getParameter("caseId"));
		int fieldId = Integer.parseInt(request.getParameter("fieldId"));
		String value = request.getParameter("value");
		TMatrixY head = tMatrixYMapper.selectByCaseIdAndFieldId(null, fieldId);
		TMatrixY rec = new TMatrixY();
		rec.setCaseid(caseId);
		rec.setFid(fieldId);
		rec.setName(head.getName());
		rec.setValue(value);
		TMatrixY tMatrixY = tMatrixYMapper.selectByCaseIdAndFieldId(caseId,
				fieldId);
		if (tMatrixY == null) {
			// insert
			tMatrixYMapper.insert(rec);
		} else {
			// update
			rec.setId(tMatrixY.getId());
			tMatrixYMapper.updateByPrimaryKeyWithBLOBs(rec);
		}
		res.setSuccessful(true);
		return res;
	}

	@RequestMapping(value = "/updateMatrixZ", method = RequestMethod.POST)
	@ResponseBody
	public Result updateMatrixZ(HttpServletRequest request) {
		Result res = new Result();
		int caseId = Integer.parseInt(request.getParameter("caseId"));
		int fieldId = Integer.parseInt(request.getParameter("fieldId"));
		String value = request.getParameter("value");
		TMatrixZWithBLOBs head = tMatrixZMapper.selectByCaseIdAndFieldId(null,
				fieldId);
		TMatrixZWithBLOBs rec = new TMatrixZWithBLOBs();
		rec.setCaseid(caseId);
		rec.setFid(fieldId);
		rec.setName(head.getName());
		rec.setValueX(value);
		TMatrixZ tMatrixZ = tMatrixZMapper.selectByCaseIdAndFieldId(caseId,
				fieldId);
		if (tMatrixZ == null) {
			// insert
			tMatrixZMapper.insert(rec);
		} else {
			// update
			rec.setId(tMatrixZ.getId());
			tMatrixZMapper.updateByPrimaryKeyWithBLOBs(rec);
		}

		// update table_schemaId
		Case _case = caseMapper.selectByPrimaryKey(caseId);
		tableMapper.updateValue("table_" + _case.getSchemaId(), value, fieldId,
				caseId);
		res.setSuccessful(true);
		return res;
	}
}
